﻿using EntitiesLayer;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL.Repositorios
{
    public class TipoMaquinaRepository : IRepositoryConsultar<TipoMaquina>
    {

        public IEnumerable<TipoMaquina> GetAll()
        {
            SqlCommand cmd = new SqlCommand();
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "PaListarTipoMaquina");

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                yield return crearTipoMaquina(dr, Convert.ToInt32(dr["IdTipoMaquina"]));
            }
        }

        private TipoMaquina crearTipoMaquina(DataRow dr, int pId)
        {
            TipoMaquina objTipoMaquina = new TipoMaquina(
                      pId,
                      dr["DescripcionTipo"].ToString()
                  );

            return objTipoMaquina;
        }
        public TipoMaquina GetById(int id)
        {
            TipoMaquina objTipoMaquina = null;
            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.Add(new SqlParameter("@IdTipoMaquina", id));
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "PaConsultarTipoMaquinaPorID");

            foreach (DataRow dr in ds.Tables[0].Rows)
                objTipoMaquina = crearTipoMaquina(dr, id);

            return objTipoMaquina;
        }
    }
}
